package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsCommentDto;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ArticleConfigService {
    @Override
    public void updateEnable(Map map) {
        //0 下架 1 上架
        Object enable = map.get("enable");
        boolean isDown = true;
        if (enable.equals(1)) {
            isDown = false;
        }
        //修改文章配置
        update(Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId, map.get("articleId")).set(ApArticleConfig::getIsDown, isDown));

    }

    /**
     * 修改评论权限
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult update_comment_status(WmNewsCommentDto dto) {
        LambdaUpdateWrapper<ApArticleConfig> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ApArticleConfig::getArticleId, dto.getArticleId()).set(ApArticleConfig::getIsComment, dto.getOperation());
        update(lqw);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * @param articleId
     * @return
     */
    @Override
    public Boolean isComment(Long articleId) {
        LambdaQueryWrapper<ApArticleConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ApArticleConfig::getArticleId, articleId);
        ApArticleConfig articleConfig = getOne(lqw);

        //判断是否能评论
        if (articleConfig == null || !articleConfig.getIsComment()) {
            return false;
        }
        return true;
    }
}
